package com.yjhh.chdataservice.controller;

import com.yjhh.chdataservice.beans.*;
import com.yjhh.chdataservice.domain.PolicyInfo;
import com.yjhh.chdataservice.except.YJHHException;
import com.yjhh.chdataservice.service.FZJCObjService;
import com.yjhh.chdataservice.service.FZJCPlacementPointService;
import com.yjhh.chdataservice.service.PublicSearchService;
import com.yjhh.chdataservice.util.BaseResultData;
import com.yjhh.chdataservice.util.CommonResultData;
import com.yjhh.chdataservice.util.PageVO;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.List;

@Api(tags = "公开查询后端管理相关接口")
@RestController
@RequestMapping(value = "/service/public/searchAdmin")
@Log4j2
public class PublicSearchAdminController extends BaseControler {

    @Autowired
    private PublicSearchService publicSearchService;

    @ApiOperation(value = "保存政策信息", notes = "保存政策信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "政策标题", required = true, paramType = "query", dataType = "String", defaultValue = "测试政策"),
            @ApiImplicitParam(name = "secondTitle", value = "政策副标题", required = false, paramType = "query", dataType = "String", defaultValue = "政策副标题"),
            @ApiImplicitParam(name = "icon", value = "新闻icon", required = false, paramType = "query", dataType = "String", defaultValue = ""),
            @ApiImplicitParam(name = "policyType", value = "政策类型 (1:中省政策;2:易地搬迁政策;3：新闻类)", required = true, paramType = "query", dataType = "Integer", defaultValue =
                    "1"),
            @ApiImplicitParam(name = "contentType", value = "内容类型 (1:文本类型;2:链接地址;3：PDF附件类型)", required = true, paramType = "query", dataType = "Integer", defaultValue =
                    "1"),
            @ApiImplicitParam(name = "content", value = "政策内容", required = false, paramType = "query", dataType = "String", defaultValue =
                    "")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/savePolicy", method = RequestMethod.POST)
    public BaseResultData savePolicy(HttpServletRequest request,
                                     @RequestParam String title, @RequestParam String secondTitle, @RequestParam String icon, @RequestParam Integer policyType, @RequestParam Integer contentType,
                                     @RequestParam String content) {
        return publicSearchService.savePolicyInfo(getRemortIP(request), title, secondTitle, icon, policyType, contentType, content);
    }

    @ApiOperation(value = "修改政策信息", notes = "修改政策信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "政策ID", required = true, paramType = "query", dataType = "String", defaultValue = "ebe916b3fcb2495b84877fa7c3609fb4"),
            @ApiImplicitParam(name = "title", value = "政策标题", required = true, paramType = "query", dataType = "String", defaultValue = "测试政策"),
            @ApiImplicitParam(name = "secondTitle", value = "政策副标题", required = false, paramType = "query", dataType = "String", defaultValue = "政策副标题"),
            @ApiImplicitParam(name = "icon", value = "新闻icon", required = false, paramType = "query", dataType = "String", defaultValue = ""),
            @ApiImplicitParam(name = "policyType", value = "政策类型 (1:中省政策;2:易地搬迁政策;3：新闻类)", required = true, paramType = "query", dataType = "Integer", defaultValue =
                    "1"),
            @ApiImplicitParam(name = "contentType", value = "内容类型 (1:文本类型;2:链接地址;3：PDF附件类型)", required = true, paramType = "query", dataType = "Integer", defaultValue =
                    "1"),
            @ApiImplicitParam(name = "content", value = "政策内容", required = false, paramType = "query", dataType = "String", defaultValue =
                    "")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/updatePolicy", method = RequestMethod.POST)
    public BaseResultData updatePolicy(HttpServletRequest request,
                                       @RequestParam String id, @RequestParam String title, @RequestParam String secondTitle, @RequestParam String icon, @RequestParam Integer policyType, @RequestParam Integer contentType,
                                       @RequestParam String content) {
        return publicSearchService.updatePolicy(getRemortIP(request), id, title, secondTitle, icon, policyType, contentType, content);
    }

    @ApiOperation(value = "获取政策信息列表按分页", notes = "获取政策信息列表按分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "请求页码从0开始", required = true, paramType = "query", dataType = "Integer", defaultValue = "0"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示的条数", required = true, paramType = "query", dataType = "Integer", defaultValue = "10"),
            @ApiImplicitParam(name = "policyType", value = "政策类型 (0：表示全部，1:中省政策;2:易地搬迁政策;)", required = true, paramType = "query", dataType = "Integer", defaultValue = "0")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/findPolicyByPaging", method = RequestMethod.GET)
    public CommonResultData<PageVO<PolicyInfoVo>> findPolicyByPaging(@RequestParam Integer pageIndex, @RequestParam Integer pageSize, @RequestParam Integer policyType) {
        return publicSearchService.findPolicyByPaging(pageIndex, pageSize, policyType);
    }

    @ApiOperation(value = "获取政策详细信息", notes = "获取政策详细信息")
    @ApiImplicitParam(name = "id", value = "政策ID", required = true, paramType = "query", dataType = "Integer", defaultValue = "38d31ec677f343848f31fc06d4526563")
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功")})
    @RequestMapping(value = "/findPolicyInfo", method = RequestMethod.GET)
    public CommonResultData<PolicyInfo> findPolicyInfo(@RequestParam String id) {
        return publicSearchService.findPolicyInfo(id);
    }

    @ApiOperation(value = "上传政策附件接口", notes = "上传政策附件接口")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @ApiImplicitParams(@ApiImplicitParam(dataType = "file", name = "file", paramType = "body"))
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public CommonResultData<String> uploadFile(HttpServletRequest request,
                                               @RequestParam MultipartFile file) {
        CommonResultData<String> commonResultData = null;
        try {
            request.setCharacterEncoding("utf-8");
            commonResultData = publicSearchService.uploadFile(getRemortIP(request), file);
        } catch (IOException e) {
            log.error("uploadFile", e);
            commonResultData = new CommonResultData<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "upload fail");
        }
        return commonResultData;
    }

    @ApiOperation(value = "删除政策", notes = "删除政策")
    @ApiImplicitParam(name = "id", value = "政策ID", required = true, paramType = "query", dataType = "Integer", defaultValue = "ebe916b3fcb2495b84877fa7c3609fb4")
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功")})
    @RequestMapping(value = "/delPolicy", method = RequestMethod.POST)
    public CommonResultData delPolicy(@RequestParam String id) {
        return publicSearchService.delPolicy(id);
    }

}
